package com.chuanke.ckfamily.action.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;

import org.springframework.beans.factory.annotation.Autowired;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chuanke.ckfamily.dao.base.CkUtil;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.CreateSystemFamilyService;
import com.chuanke.ckfamily.service.finance.FamilyDayIncomeService;

import com.alibaba.fastjson.JSON;
import com.chuanke.ckfamily.dao.base.CkUtil;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.CreateSystemFamilyService;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.finance.FamilyDayIncomeService;
import com.chuanke.ckfamily.service.finance.PacketFinaceService;
import com.chuanke.ckfamily.service.social.FamilyFinanceService;
import com.chuanke.ckfamily.service.social.FamilyService;

@Controller
@RequestMapping("/redis")
public class ClearRedisController extends HttpServlet {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired

	private CacheService cacheService;

	@Autowired
	private FamilyDayIncomeService familyDayIncomeService;

	@Autowired
	private FamilyService familyService;

	@Autowired
	private FamilyFinanceService familyFinanceService;

	@Autowired
	private PacketFinaceService packetFinaceService;

	@Autowired
	private CreateSystemFamilyService createSystemFamilyService;

	@RequestMapping("clear")
	@ResponseBody
	public String clear(@RequestParam(value = "key") String key) {
		if (key.endsWith("*")) {
			key = key.substring(0, key.length() - 1);
			cacheService.deleteByPrex(key);
		} else {
			cacheService.delete(key);
		}
		return "true";
	}
	/*
	 * @RequestMapping("flush")
	 * 
	 * @ResponseBody public String flush() { cacheService.flushDb(); return
	 * "true"; }
	 * 
	 * @RequestMapping("get")
	 * 
	 * @ResponseBody public String get(@RequestParam(value = "key") String key)
	 * { Object object = cacheService.get(key); return
	 * JSON.toJSONString(object); }
	 * 
	 * @RequestMapping("create")
	 * 
	 * @ResponseBody public String create(@RequestParam(value = "key") String
	 * key) { try { if ("abc123".equals(key)) {
	 * createSystemFamilyService.initHead(); } } catch (Exception e) {
	 * e.printStackTrace(); } return "true"; }
	 */

	@RequestMapping("count")
	@ResponseBody
	public String count() {
		try {
			// familyDayIncomeService.countIncome(DateFormatUtils.format(new
			// Date(), DateFormatUtils.ISO_DATE_FORMAT.getPattern()));
			familyDayIncomeService.countIncome(CkUtil.getYesterdayStr());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "true";

	}

	@RequestMapping("initf")
	@ResponseBody
	public String initf() {

		Map<String, Object> param = new HashMap<String, Object>();
		List<Map<String, Object>> list = familyService.search(param);
		for (Map<String, Object> map : list) {
			long fid = MapUtils.getLongValue(map, "id");
			Long type = MapUtils.getLongValue(map, "type");
			if (type.equals(1l)) {
				logger.debug("initf==========>fid={},type={},3000", fid, type);
				packetFinaceService.addForSystem(fid, 3000l);
			} else if (type.equals(2l)) {
				logger.debug("initf==========>fid={},type={},10000", fid, type);
				packetFinaceService.addForSystem(fid, 10000l);
			}

		}
		return "true";
	}

	@RequestMapping("up_owner")
	@ResponseBody
	public String up_owner() throws CFException {
		Long[] ids1 = { 70l, 344195l };
		Long[] ids2 = { 64l, 327135l };
		Long[] ids3 = { 60l, 358249l };
		Long[] ids4 = { 734030l, 331147l };
		Long[] ids5 = { 725493l, 697584l };

		List<Long[]> list = new ArrayList<>();

		list.add(ids1);
		list.add(ids2);
		list.add(ids3);
		list.add(ids4);
		list.add(ids5);
		for (Long[] ids : list) {
			createSystemFamilyService.updateOwner(ids);
		}

		return "true";
	}

}
